<?php

class Statement {

	/**
	 * create connection to database
	 */
	public function connection() {

		if (! ($this->connection = mysql_connect( SERVER_NAME, USER_NAME, PASSWORD))) {
			echo mysql_error();
		}
		mysql_query( 'SET NAMES UTF-8');
		mysql_set_charset('utf8',$this->connection);
		return $this->connection;
	}

	/**
	 * close connectin with database 
	 */
	public function close() {

		mysql_close( $this->connection);
	}

	/**
	 * execute query upadte
	 * @return true in case update database seuccess, other whise return false
	 */
	public function executeQueryUpdate($query) {

		$execute = FALSE;
		$this->connection = $this->connection();
		mysql_query( 'SET NAMES UTF-8');
		mysql_select_db( DATABASE, $this->connection);
		mysql_query( 'SET NAMES UTF-8');
		if (! mysql_query( $query, $this->connection)) {
			echo mysql_error();
			$execute = FALSE;
		} else {
			$execute = TRUE;
		}
		$this->close();
		return $execute;
	}

	/**
	 *	execute select query
	 *	@return null when select false, other hand, return resultset 
	 */
	public function executeQuerySelect($query) {

		$this->connection = $this->connection();
		mysql_query( 'SET NAMES UTF-8');
		mysql_select_db( DATABASE, $this->connection);
		if (! ($result = mysql_query( $query, $this->connection))) {
			echo mysql_error();
			$this->close();
			return NULL;
		}
		$this->close();
		return $result;
	}
}
?>